package com.jams.goodox.activity.serve.headhunter;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hangzhou.jin.customview.TextMoreLayout;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.activity.common.ChooseListActivity;
import com.jams.goodox.entity.ChooseItem;
import com.jams.goodox.entity.LinkManBean;
import com.jams.goodox.entity.User;
import com.jams.goodox.entity.resp.LinkManResp;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.HeadHunterService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.DataCacheManager;
import com.jams.goodox.utils.EditTextUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * * 招聘联系人维护
 *
 * create by 顾佳佳 on 2017/05/17
 */
public class JobConnecterEditActivity extends BaseActivity {

    @Bind(R.id.tv_connecter_name)
    TextView tvConnecterName;
    @Bind(R.id.et_connecter_name)
    EditText etConnecterName;
    @Bind(R.id.et_connecter_phone)
    EditText etConnecterPhone;
    @Bind(R.id.et_connecter_email)
    EditText etConnecterEmail;
    @Bind(R.id.et_company_do)
    EditText etCompanyDo;
    @Bind(R.id.tl_company_status)
    TextMoreLayout tlCompanyStatus;
    @Bind(R.id.tl_company_people)
    TextMoreLayout tlCompanyPeople;

    public static final int StageRequest = 102;//融资阶段回调
    public static final int PersonsRequest = 103;//公司人数回调
    private List<ChooseItem> stageList;
    private List<ChooseItem> personList;
    int stagePosition = -1;
    int personPosition = -1;
    private LinkManBean linkManBean;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_job_coonter_edit;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, getString(R.string.connect_people_info_edit));
        tvConnecterName.setFocusable(true);
        tvConnecterName.setFocusableInTouchMode(true);
        tvConnecterName.requestFocus();
        stageList = CollectionUtils.arrayToList(getResources().getStringArray(R.array.company_stage),-1);
        personList = CollectionUtils.arrayToList(getResources().getStringArray(R.array.company_persons),-1);
        getLinkManInfo();
    }

    @OnClick({R.id.tl_company_status, R.id.tl_company_people, R.id.tv_save})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.tl_company_status:
                intent = new Intent(this, ChooseListActivity.class);
                intent.putExtra(ChooseListActivity.TITLE,"公司所处阶段");
                intent.putExtra(ChooseListActivity.CHOOSE, (Serializable)CollectionUtils.arrayToList(getResources().getStringArray(R.array.company_stage),stagePosition));
                startActivityForResult(intent,StageRequest);
                break;
            case R.id.tl_company_people:
                intent = new Intent(this, ChooseListActivity.class);
                intent.putExtra(ChooseListActivity.TITLE,"公司人数");
                intent.putExtra(ChooseListActivity.CHOOSE, (Serializable) CollectionUtils.arrayToList(getResources().getStringArray(R.array.company_persons),personPosition));
                startActivityForResult(intent,PersonsRequest);
                break;
            case R.id.tv_save:
                if(checkInput()){
                    save();
                }
                break;
        }
    }

    private void getLinkManInfo(){
        User user = DataCacheManager.getInstance().getUser();
        if(user == null || TextUtils.isEmpty(user.getEnterpriseId())){
            return;
        }
        enqueue(createCallService(HeadHunterService.class).getLinkManInfo(user.getEnterpriseId()), new RetrofitCallbackListener<LinkManResp>() {
            @Override
            public void onSuccess(Call<LinkManResp> call, LinkManResp result) {
                linkManBean = result.getData();
                if(linkManBean != null){
                    etConnecterName.setText(linkManBean.getLinkmanName());
                    etConnecterPhone.setText(linkManBean.getConnection());
                    etConnecterEmail.setText(linkManBean.getEmail());
                    etCompanyDo.setText(linkManBean.getIndustry());
                    tlCompanyStatus.setRightText(stageList.get(linkManBean.getLevel()).getName());
                    tlCompanyPeople.setRightText(personList.get(linkManBean.getCount()).getName());
                    stagePosition = linkManBean.getLevel();
                    personPosition = linkManBean.getCount();
                }
            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    //保存
    private void save(){
        User user = DataCacheManager.getInstance().getUser();
        if(user == null || TextUtils.isEmpty(user.getEnterpriseId())){
            return;
        }
        if(linkManBean == null){
            linkManBean = new LinkManBean();
        }
        linkManBean.setLinkmanName(etConnecterName.getText().toString());
        linkManBean.setConnection(etConnecterPhone.getText().toString());
        linkManBean.setEmail(etConnecterEmail.getText().toString());
        linkManBean.setIndustry(etCompanyDo.getText().toString());
        linkManBean.setLevel(stagePosition);
        linkManBean.setCount(personPosition);
        linkManBean.setEnterpriseId(user.getEnterpriseId());
        linkManBean.setId(linkManBean.getId());
        String strJson = new Gson().toJson(linkManBean);
        enqueue(createCallService(HeadHunterService.class).saveLinkManInfo(strJson), new RetrofitCallbackListener<BaseEntity>() {
            @Override
            public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                ToastUtil.show("保存成功");
                finish();
            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    //检查输入
    private boolean checkInput(){
        if(EditTextUtils.editTextIsEmpty(etConnecterName)){
            ToastUtil.show("请输入联系人名称");
            return false;
        }
        if(EditTextUtils.editTextIsEmpty(etConnecterPhone)){
            ToastUtil.show("请填写联系电话");
            return false;
        }
        if(EditTextUtils.editTextIsEmpty(etConnecterEmail)){
            ToastUtil.show("请填写接收简历邮箱");
            return false;
        }
        if(EditTextUtils.editTextIsEmpty(etCompanyDo)){
            ToastUtil.show("请填写公司行业");
            return false;
        }
        if(stagePosition == -1){
            ToastUtil.show("请选择公司融资阶段");
            return false;
        }
        if(personPosition == -1){
            ToastUtil.show("请选择公司人数");
            return false;
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 100){
            switch (requestCode){
                case StageRequest://选择公司融资阶段回调
                    if(data != null){
                        ChooseItem chooseItem = (ChooseItem) data.getSerializableExtra(ChooseListActivity.CHOOSE);
                        tlCompanyStatus.setRightText(chooseItem.getName());
                        stagePosition = chooseItem.getIds();
                    }
                    break;
                case PersonsRequest://选择公司人数回调
                    if(data != null){
                        ChooseItem chooseItem = (ChooseItem) data.getSerializableExtra(ChooseListActivity.CHOOSE);
                        tlCompanyPeople.setRightText(chooseItem.getName());
                        personPosition = chooseItem.getIds();
                    }
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        cancelAllCall();
        super.onDestroy();
    }

}
